/**
 * 
 */
package sailing.ui.model;

import java.util.List;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.Cashier;
import sailing.listener.DataListener;
import sailing.managers.CashierManager;
import cjlite.utils.Lists;

/**
 * @author kevin
 * 
 */
public class CashierTableModel extends SailingTableModel implements DataListener {

	private String[] columnName = { "经手人", "操作" };

	private List<Cashier> cashierList = Lists.newArrayList();

	private boolean showEditBtn = false;

	/**
	 * @param application
	 */
	public CashierTableModel(SingleFrameApplication application) {
		super(application);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return this.cashierList.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return this.showEditBtn ? this.columnName.length : this.columnName.length - 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (rowIndex >= this.cashierList.size()) {
			return "";
		}

		Cashier c = this.cashierList.get(rowIndex);

		switch (columnIndex) {
		case 0:
			return c.getName();
		case 1:
			return c;
		}

		return "";

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.model.SailingTableModel#updateModel()
	 */
	@Override
	public void updateModel() {
		CashierManager cm = this.getApplication().getContext().getIoc().getInstance(CashierManager.class);
		cashierList.clear();
		cashierList.addAll(cm.getAllCashierList());
		super.fireTableDataChanged();
	}

	@Override
	public String getColumnName(int column) {
		return columnName[column];
	}

	public void showEditBtn(boolean selected) {
		this.showEditBtn = selected;
	}

	@Override
	public void dataChanged() {
		this.updateModel();
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return columnIndex == this.columnName.length - 1;
	}

}
